package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.LogManager;
import com.informationsystem.service.SystemuserManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.Log;
import com.informationsystem.webapp.action.BaseAction;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class LogAction extends BaseAction implements Preparable {
    private LogManager logManager;
    private List logs;
    private Log log;
    private Long id;
    private String query;
    private Date hourformat;

    public void setLogManager(LogManager logManager) {
        this.logManager = logManager;
    }

    public List getLogs() {
        return logs;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String logId = getRequest().getParameter("log.id");
            if (logId != null && !logId.equals("")) {
                log = logManager.get(new Long(logId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            logs = logManager.search(query, Log.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            logs = logManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Log getLog() {
        return log;
    }

    public void setLog(Log log) {
        this.log = log;
    }

    public String delete() {
        logManager.remove(log.getId());
        saveMessage(getText("log.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            log = logManager.get(id);
        } else {
            log = new Log();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (log.getId() == null);
        log.setLogDate(generateLogDate());
        logManager.save(log);

        String key = (isNew) ? "log.added" : "log.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public SystemuserManager getSystemuserManager() {
		return systemuserManager;
	}

	public void setSystemuserManager(SystemuserManager systemuserManager) {
		this.systemuserManager = systemuserManager;
	}
	
	public List getSystemuserList(){
		return systemuserManager.getAll();
	}
	
	public Date getHourformat() {
		return log.getLogDate();
	}

	public void setHourformat(Date hourformat) {
		this.hourformat = hourformat;
	}
	
	@SuppressWarnings("deprecation")
	public Date generateLogDate(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(log.getLogDate()); 
		cal.set(Calendar.HOUR_OF_DAY, hourformat.getHours());
		cal.set(Calendar.MINUTE,hourformat.getMinutes());

		return cal.getTime();
	}
}